package com.xg.serviceOrder.interceptor;

import com.xg.common.constant.TokenConstant;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 请求拦截器
 */
public class AuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Cookie[] cookies = request.getCookies();
        String successToken = TokenConstant.SUCCESS_TOKEN;

        String token = null;
        for (Cookie cookie : cookies) {
            if (successToken.equals(cookie.getName())){
                token = cookie.getValue();
            }
        }

        if (null == token){
            response.sendRedirect("http://localhost:9003/user/toLogin");
        }
        return true;
    }
}
